package cc.comac.mom.datahandlers.entity;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cc.comac.mom.bean.*;
import cc.comac.mom.dao.entity.*;
import cc.comac.mom.entity.*;
import cc.comac.mom.util.type.RepairInfoPlanTypeMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.BaseConnectionData;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.BeanManager;
import cc.comac.mom.util.MomConstants;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

@Component
@Transactional
public class EquipRepairDataHandler extends ServiceImpl<EquipRepairDao, EquipRepairEntity> {
	@Autowired
	private EquipRepairDao equipRepairDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private EquipFaultlCauseDao equipFaultlCauseDao;
	@Autowired
	private EquipMaintainWorkOrderDao equipMaintainWorkOrderDao;
	@Autowired
	private EquipRegularCheckWorkOrderDao equipRegularCheckWorkOrderDao;
	@Autowired
	private EquipPatrolCheckWorkOrderDao equipPatrolCheckWorkOrderDao;
	@Autowired
	private EquipOperatorDataHandler equipOperatorDataHandler;
	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;
	@Resource
	private IdFactory idFactory;

	@Autowired
	private EquipDevDao equipDevDao;

	@Autowired
	private EquipRepairOpRecordDao equipRepairOpRecordDao;

	@Autowired
	private EquipDevHistoryDataHandler equipDevHistoryDataHandler;
	@Autowired
	private EquipRepairStepDao equipRepairStepDao;

	@Autowired
	private BaseConnectionData baseConnectionData;

	// 维修知识库查询已验收的维修信息
	public ResultDev selectByDevNumberAndFaultCause(EquipRepairBean obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		if (obj.getPageNum() > 0) {
			obj.setPageNum((obj.getPageNum() - 1) * 10);
		}

		List<EquipRepairBean> listEquipRepairBean = equipRepairDao.selectByDevNumberAndFaultCause(obj);
		int count = equipRepairDao.selectCountByDevNumberAndFaultCause(obj);
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double) count / obj.getPageSize());
		resultDev.setEntityList(listEquipRepairBean);
		resultDev.setPageCount((int) pageCount);
		resultDev.setTotalQuantity(count);
		return resultDev;
	}

	// 修改故障原因
	public ResultBean updateFaultCauseById(EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairEntity innerData = obj.getInnerData();
		if (innerData == null) {
			throw new RuntimeException("数据为空");
		}
		if (innerData.getFaultCauseId() == 0) {
			throw new RuntimeException("故障原因为空");
		}
		EquipRepairEntity repairEntity = equipRepairDao.selectById(innerData.getId());
		if (repairEntity == null) {
			throw new RuntimeException("修改的数据不存在");
		}
		long faultCauseIdOld = repairEntity.getFaultCauseId();
		long faultCauseIdNew = innerData.getFaultCauseId();
		EquipFaultCauseEntity faultCauseEntityOld = equipFaultlCauseDao.selectById(faultCauseIdOld);
		if (faultCauseEntityOld == null) {
			throw new RuntimeException("原故障原因不存在");
		}

		EquipFaultCauseEntity faultCauseEntityNew = equipFaultlCauseDao.selectById(faultCauseIdNew);
		if (faultCauseEntityNew == null) {
			throw new RuntimeException("新故障原因不存在");
		}

		repairEntity.setFaultCauseId(innerData.getFaultCauseId());
		baseEntityDataUtil.updateBaseEntityData(repairEntity);
		int update = equipRepairDao.updateById(repairEntity);
		if (update < 1) {
			throw new RuntimeException("修改故障原因失败");
		}
		// 保存操作记录
		EquipOperatorEntity equipOperatorEntity = new EquipOperatorEntity();
		equipOperatorEntity.setOperation("维修信息故障原因-修改");
		baseEntityDataUtil.setBaseEntityData(equipOperatorEntity);
		equipOperatorEntity.setContent(repairEntity.getPlanNumber() + "," + repairEntity.getDevNumber() + ","
				+ obj.getDevName() + " 故障原因:\"" + faultCauseEntityOld.getFaultCauseDescribe() + "\" 修改为: \""
				+ faultCauseEntityNew.getFaultCauseDescribe() + "\"");
		boolean save = equipOperatorDataHandler.save(equipOperatorEntity);
		if (!save) {
			throw new RuntimeException("维修知识库修改故障原因操作记录保存失败");
		}
		return resultBean;
	}

	// 通过id获取维修信息
	public EquipRepair getById(long id) {
		EquipRepairEntity selectById = equipRepairDao.selectById(id);
		if (selectById == null) {
			return null;
		}
		EquipRepair equipRepair = new EquipRepair();
		equipRepair.setInnerData(selectById);
		if (equipRepair.getInnerData().getAttachmentGroupId() != 0) {
			List<QualityAttachmentGroup> listQualityAttachmentGroup = qualityAttachmentGroupDataHandler
					.getByAttachmentGroupId(equipRepair.getInnerData().getAttachmentGroupId());
			equipRepair.setListAttachmentGroup(listQualityAttachmentGroup);
		}
		return equipRepair;
	}

	// 故障提报
	public ResultBean insetFaultInfo(EquipRepair obj) {
		String opContent = "故障提报：";
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairEntity innerData = obj.getInnerData();
		if (StringUtils.isEmpty(innerData.getPlanType())) {
			throw new RuntimeException("计划类型为空");
		}

		if (StringUtils.isEmpty(innerData.getDevNumber())) {
			throw new RuntimeException("设备编号为空");
		}
		innerData.setPlanNumber(idFactory.generateEquipRepairNo());
		innerData.setStatus(MomConstants.EQUIP_REPAIR_STATUS_1);
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipRepairDao.insert(innerData);
		if (insert < 1) {
			throw new RuntimeException("保存故障提报失败");
		}
		// 修改主数据状态 使用中
		EquipDevEntity equipDevEntity = equipDevDao.selectById(innerData.getDevId());
		if (equipDevEntity == null) {
			throw new RuntimeException("无法获取设备主数据信息");
		}
		equipDevEntity.setDevStatus(MomConstants.EQUIP_STATUS_USEING);
		baseEntityDataUtil.updateBaseEntityData(equipDevEntity);
		int updateById2 = equipDevDao.updateById(equipDevEntity);
		if (updateById2 < 1) {
			throw new RuntimeException("修改设备状态失败");
		}

		// 记录设备履历数据
		EquipDevHistory equipDevHistory = new EquipDevHistory();
		EquipDevHistoryEntity equipDevHistoryEntity = new EquipDevHistoryEntity();
		equipDevHistory.setInnerData(equipDevHistoryEntity);
		// 通过设备编号查询设备信息
		QueryWrapper<EquipDevEntity> queryWrapper2 = new QueryWrapper<EquipDevEntity>();
		queryWrapper2.eq("row_status", 1);
		queryWrapper2.eq("dev_number", innerData.getDevNumber());
		EquipDevEntity selectOne = equipDevDao.selectOne(queryWrapper2);
		equipDevHistoryEntity.setDevId(selectOne.getId());
		equipDevHistoryEntity.setDevNumber(selectOne.getDevNumber());
		equipDevHistoryEntity.setOperationDetail(innerData.getPlanNumber());
		equipDevHistoryEntity.setOperationStyle(RepairInfoPlanTypeMap.getTypeCN(innerData.getPlanType())+"_故障提报");
		equipDevHistoryEntity.setRecordTime(innerData.getFaultDate());
		equipDevHistoryEntity.setOperator(baseEntityDataUtil.getCurrentUser());
		baseEntityDataUtil.setBaseEntityData(equipDevHistoryEntity);
		equipDevHistoryDataHandler.insertHistory(equipDevHistory);

		// 保存操作记录
		opContent = opContent + "维修单号为:" + innerData.getPlanNumber();
		EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
		equipRepairOpRecordEntity.setRecordContent(opContent);
		equipRepairOpRecordEntity.setRepairId(innerData.getId());
		baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
		int insert2 = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
		if (insert2 < 1) {
			throw new RuntimeException("保存操作记录失败");
		}
		return resultBean;
	}

	// 派工
	public ResultBean dispatch(EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		String opContent = "派工：";
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		if (StringUtils.isEmpty(obj.getInnerData().getActualUser())) {
			throw new RuntimeException("执行人为空");
		}
		EquipRepairEntity selectById = equipRepairDao.selectById(obj.getInnerData().getId());
		if (selectById == null) {
			throw new RuntimeException("该维修信息不存在");
		}
		if (StringUtils.isEmpty(obj.getInnerData().getRepairPersonnel())) {
			selectById.setRepairPersonnel(obj.getInnerData().getRepairPersonnel());
		}
		selectById.setStatus(MomConstants.EQUIP_REPAIR_STATUS_2);
		selectById.setActualUser(obj.getInnerData().getActualUser());
		selectById.setActualUserJobNumber(obj.getInnerData().getActualUserJobNumber());
		selectById.setExpectedCompletionDate(obj.getInnerData().getExpectedCompletionDate());
		baseEntityDataUtil.updateBaseEntityData(selectById);
		int updateById = equipRepairDao.updateById(selectById);
		if (updateById < 1) {
			throw new RuntimeException("派工失败");
		}
		// 保存操作记录
		opContent = opContent + "维修单号:" + selectById.getPlanNumber() + " 执行人为:" + obj.getInnerData().getActualUser();
		EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
		equipRepairOpRecordEntity.setRecordContent(opContent);
		equipRepairOpRecordEntity.setRepairId(selectById.getId());
		baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
		int insert = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
		if (insert < 1) {
			throw new RuntimeException("保存操作记录失败");
		}
		return resultBean;
	}

	// 分配
	public ResultBean allot(EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		String opContent = "维修分配：";
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		if (StringUtils.isEmpty(obj.getInnerData().getRepairTeam())) {
			throw new RuntimeException("维修班组为空");
		}
		EquipRepairEntity selectById = equipRepairDao.selectById(obj.getInnerData().getId());
		if (selectById == null) {
			throw new RuntimeException("该维修信息不存在");
		}
		if (selectById.getStatus() > 3) {
			throw new RuntimeException("维修结束状态不允许分配");
		}
		opContent = opContent + "原班组:" + selectById.getRepairTeam() + "修改为" + obj.getInnerData().getRepairTeam();
		selectById.setRepairTeam(obj.getInnerData().getRepairTeam());
		selectById.setRepairTeamId(obj.getInnerData().getRepairTeamId());
		selectById.setActualUser("");// 实际维修人清空
		selectById.setActualUserJobNumber("");// 实际维修人工号清空
		selectById.setStatus(MomConstants.EQUIP_REPAIR_STATUS_1);// 故障中
		baseEntityDataUtil.updateBaseEntityData(selectById);
		int updateById = equipRepairDao.updateById(selectById);
		if (updateById < 1) {
			throw new RuntimeException("分配失败");
		}
		// 保存操作记录
		EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
		equipRepairOpRecordEntity.setRecordContent(opContent);
		equipRepairOpRecordEntity.setRepairId(selectById.getId());
		baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
		int insert = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
		if (insert < 1) {
			throw new RuntimeException("保存操作记录失败");
		}
		return resultBean;
	}

	// 验收
	public ResultBean acceptance(EquipRepair obj) {
		String opContents = "验收:";
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairEntity selectById = equipRepairDao.selectById(obj.getInnerData().getId());
		if (selectById == null) {
			throw new RuntimeException("该维修信息不存在");
		}
		if (StringUtils.isEmpty(obj.getInnerData().getTestResult())) {
			throw new RuntimeException("试运行情况为空");
		}
		if (StringUtils.isEmpty(obj.getInnerData().getRepairAffectPrecision())) {
			throw new RuntimeException("是否影响设备精度为空");
		}
		if (obj.getInnerData().getRepairAffectPrecision().equals("1")) {
			// 影响精度，质量检验必填
			if (StringUtils.isEmpty(obj.getInnerData().getTestQualityCheck())) {
				throw new RuntimeException("首件质量检验为空");
			}
			if (StringUtils.isEmpty(obj.getInnerData().getInitialQualityCheck())) {
				throw new RuntimeException("试件质量检验为空");
			}
		}
		if (obj.getInnerData().getAcceptanceDate() == null) {
			selectById.setAcceptanceDate(TimeUtil.getLocalDate());
		} else {
			selectById.setAcceptanceDate(obj.getInnerData().getAcceptanceDate());
		}
		selectById.setRepairAffectPrecision(obj.getInnerData().getRepairAffectPrecision());
		selectById.setTestResult(obj.getInnerData().getTestResult());
		selectById.setInitialQualityCheck(obj.getInnerData().getInitialQualityCheck());
		selectById.setTestQualityCheck(obj.getInnerData().getTestQualityCheck());
		selectById.setAcceptanceResult(obj.getInnerData().getAcceptanceResult());
		selectById.setRemadeFlag(obj.getInnerData().getRemadeFlag());
		selectById.setStatus(MomConstants.EQUIP_REPAIR_STATUS_5);
		selectById.setAcceptanceUser(obj.getInnerData().getAcceptanceUser());
		selectById.setAcceptanceUserJobNumber(obj.getInnerData().getAcceptanceUserJobNumber());
		selectById.setAcceptanceDescribe(obj.getInnerData().getAcceptanceDescribe());

		int updateById = equipRepairDao.updateById(selectById);
		if (updateById < 1) {
			throw new RuntimeException("验收失败");
		}
		// 如果返工，则重新生成维修单
		if (obj.getInnerData().getRemadeFlag() == 1) {
			String opContent = "验收返工:";
			EquipRepairEntity equipRepairEntity = new EquipRepairEntity();
			equipRepairEntity.setPlanNumber("FG" + idFactory.generateEquipRepairNo());
			equipRepairEntity.setPlanType(selectById.getPlanType());
			equipRepairEntity.setDevId(selectById.getDevId());
			equipRepairEntity.setDevNumber(selectById.getDevNumber());
			equipRepairEntity.setDevName(selectById.getDevName());
			equipRepairEntity.setDevLocation(selectById.getDevLocation());
			equipRepairEntity.setDevWorkShop(selectById.getDevWorkShop());
			equipRepairEntity.setMaintenanceTeam(selectById.getRepairTeam());
			equipRepairEntity.setRepairPersonnel(selectById.getActualUser());
			equipRepairEntity.setRepairPersonnelJobNumber(equipRepairEntity.getRepairPersonnelJobNumber());
			equipRepairEntity.setReportUser(selectById.getReportUser());
			equipRepairEntity.setReportJobNumber(selectById.getReportJobNumber());
			equipRepairEntity.setFaultDate(selectById.getFaultDate());
			equipRepairEntity.setFaultDisplayId(selectById.getFaultDisplayId());
			equipRepairEntity.setFaultDisplayDescribe(selectById.getFaultDisplayDescribe());
			equipRepairEntity.setFaultDescribe(selectById.getFaultDescribe());
			equipRepairEntity.setIsStop(selectById.getIsStop());
			equipRepairEntity.setIsFault(selectById.getIsFault());
			equipRepairEntity.setProductQuality(selectById.getProductQuality());
			equipRepairEntity.setAttachmentGroupId(selectById.getAttachmentGroupId());
			baseEntityDataUtil.setBaseEntityData(equipRepairEntity);
			int insert = equipRepairDao.insert(equipRepairEntity);
			if (insert < 1) {
				throw new RuntimeException("返工生成新的维修单失败");
			}
			// 修改主数据状态为故障中
			EquipDevEntity equipDevEntity = equipDevDao.selectById(selectById.getDevId());
			if (equipDevEntity == null) {
				throw new RuntimeException("无法获取设备主数据信息");
			}
			equipDevEntity.setDevStatus(MomConstants.EQUIP_STATUS_DEFECTING);
			baseEntityDataUtil.updateBaseEntityData(equipDevEntity);
			int updateById2 = equipDevDao.updateById(equipDevEntity);
			if (updateById2 < 1) {
				throw new RuntimeException("修改设备状态失败");
			}
			// 保存操作记录
			opContent = opContent + "维修工单编号:" + equipRepairEntity.getPlanNumber() + "由原维修单号:"
					+ selectById.getPlanNumber() + "返工生成";
			EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
			equipRepairOpRecordEntity.setRecordContent(opContent);
			equipRepairOpRecordEntity.setRepairId(selectById.getId());
			baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
			int insert2 = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
			if (insert2 < 1) {
				throw new RuntimeException("保存操作记录失败");
			}
		}
		// 保存操作记录
		if (obj.getInnerData().getRemadeFlag() == 1) {
			opContents = opContents + " 返工状态为:返工";
		} else {
			opContents = opContents + " 返工状态为:不返工";
		}
		if (obj.getInnerData().getAcceptanceResult() == 1) {
			opContents = opContents + " 验收结果为:合格";
		} else {
			opContents = opContents + " 验收结果为:不合格";
		}
		opContents = opContents + " 验收描述为:" + obj.getInnerData().getAcceptanceDescribe();
		EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
		equipRepairOpRecordEntity.setRecordContent(opContents);
		equipRepairOpRecordEntity.setRepairId(selectById.getId());
		baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
		int insert2 = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
		if (insert2 < 1) {
			throw new RuntimeException("保存操作记录失败");
		}
		return resultBean;
	}

	// 维修分析
	public ResultBean repairAnalyze(EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		String opContent = "保存维修分析";
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		if (StringUtils.isEmpty(obj.getInnerData().getFaultCauseDescribe())) {
			throw new RuntimeException("故障原因为空");
		}
		EquipRepairEntity selectById = equipRepairDao.selectById(obj.getInnerData().getId());
		if (selectById == null) {
			resultBean.setErrorMessge("该维修信息不存在");
			return resultBean;
		}
		selectById.setFaultCauseId(obj.getInnerData().getFaultCauseId());
		selectById.setFaultCauseDescribe(obj.getInnerData().getFaultCauseDescribe());
		selectById.setCauseDescribe(obj.getInnerData().getCauseDescribe());
		selectById.setIsOutsource(obj.getInnerData().getIsOutsource());
		selectById.setIsArtificial(obj.getInnerData().getIsArtificial());
		baseEntityDataUtil.updateBaseEntityData(selectById);
		int updateById = equipRepairDao.updateById(selectById);
		if (updateById < 1) {
			throw new RuntimeException("维修分析失败");
		}
		// 保存操作记录
		EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
		equipRepairOpRecordEntity.setRecordContent(opContent);
		equipRepairOpRecordEntity.setRepairId(selectById.getId());
		baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
		int insert2 = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
		if (insert2 < 1) {
			throw new RuntimeException("保存操作记录失败");
		}
		return resultBean;
	}

	// 维修开始
	public ResultBean repairBegin(EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String opContent = "维修开始：";
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairEntity selectById = equipRepairDao.selectById(obj.getInnerData().getId());
		if (selectById == null) {
			throw new RuntimeException("该维修信息不存在");
		}
		if (selectById.getStatus() > MomConstants.EQUIP_REPAIR_STATUS_2) {
			throw new RuntimeException("维修已经开工，不可再次开工");
		}
		if (obj.getInnerData().getActualStartDate() == null) {
			obj.getInnerData().setActualStartDate(TimeUtil.getLocalDate());
		}
		selectById.setStatus(MomConstants.EQUIP_REPAIR_STATUS_3);
		selectById.setActualStartDate(obj.getInnerData().getActualStartDate());
		selectById.setIsStop(obj.getInnerData().getIsStop());
		baseEntityDataUtil.updateBaseEntityData(selectById);
		int updateById = equipRepairDao.updateById(selectById);
		if (updateById < 1) {
			throw new RuntimeException("维修开始失败");
		}
		// 修改设备状态为：维修中
		EquipDevEntity equipDevEntity = equipDevDao.selectById(selectById.getDevId());
		if (equipDevEntity == null) {
			throw new RuntimeException("无法获取设备主数据信息");
		}
		equipDevEntity.setDevStatus(MomConstants.EQUIP_STATUS_REPAIRING);
		baseEntityDataUtil.updateBaseEntityData(equipDevEntity);
		int updateById2 = equipDevDao.updateById(equipDevEntity);
		if (updateById2 < 1) {
			throw new RuntimeException("修改设备状态失败");
		}
		// 保存操作记录
		opContent = opContent + "执行人为:" + selectById.getReportUser() + " 开始日期为:"
				+ df.format(selectById.getActualStartDate());
		EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
		equipRepairOpRecordEntity.setRecordContent(opContent);
		equipRepairOpRecordEntity.setRepairId(selectById.getId());
		baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
		int insert2 = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
		// 保存设备履历数据
		EquipDevHistory equipDevHistory = new EquipDevHistory();
		EquipDevHistoryEntity equipDevHistoryEntity = new EquipDevHistoryEntity();
		equipDevHistory.setInnerData(equipDevHistoryEntity);
		// 通过设备编号查询设备信息
		QueryWrapper<EquipDevEntity> queryWrapper2 = new QueryWrapper<EquipDevEntity>();
		queryWrapper2.eq("row_status", 1);
		queryWrapper2.eq("dev_number", selectById.getDevNumber());
		EquipDevEntity selectOne = equipDevDao.selectOne(queryWrapper2);
		equipDevHistoryEntity.setDevId(selectOne.getId());
		equipDevHistoryEntity.setDevNumber(selectOne.getDevNumber());
		equipDevHistoryEntity.setOperationDetail(selectById.getPlanNumber());
		equipDevHistoryEntity.setOperationStyle("维修");
		equipDevHistoryEntity.setRecordTime(selectById.getActualStartDate());
		equipDevHistoryEntity.setOperator(baseEntityDataUtil.getCurrentUser());
		baseEntityDataUtil.setBaseEntityData(equipDevHistoryEntity);
		equipDevHistoryDataHandler.insertHistory(equipDevHistory);

		if (insert2 < 1) {
			throw new RuntimeException("保存操作记录失败");
		}
		return resultBean;
	}

	// 维修结束
	public ResultBean repairEnd(EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String opContent = "维修结束：";
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairEntity selectById = equipRepairDao.selectById(obj.getInnerData().getId());
		if (selectById == null) {
			throw new RuntimeException("该维修信息不存在");
		}
		if (obj.getInnerData().getMaintenanceTime() == null) {
			throw new RuntimeException("维修时长不能为空");
		}
		if (obj.getInnerData().getActualEndDate() == null) {
			obj.getInnerData().setActualEndDate(TimeUtil.getLocalDate());
		}
		selectById.setActualEndDate(obj.getInnerData().getActualEndDate());
		selectById.setRecurrenceType(obj.getInnerData().getRecurrenceType());
		selectById.setMaintenanceTime(obj.getInnerData().getMaintenanceTime());
		selectById.setStatus(MomConstants.EQUIP_REPAIR_STATUS_4);
		baseEntityDataUtil.updateBaseEntityData(selectById);
		int updateById = equipRepairDao.updateById(selectById);
		if (updateById < 1) {
			throw new RuntimeException("维修结束失败");
		}
		// 修改主数据状态 使用中
		EquipDevEntity equipDevEntity = equipDevDao.selectById(selectById.getDevId());
		if (equipDevEntity == null) {
			throw new RuntimeException("无法获取设备主数据信息");
		}
		equipDevEntity.setDevStatus(MomConstants.EQUIP_STATUS_USEING);
		baseEntityDataUtil.updateBaseEntityData(equipDevEntity);
		int updateById2 = equipDevDao.updateById(equipDevEntity);
		if (updateById2 < 1) {
			throw new RuntimeException("修改设备状态失败");
		}
		// 保存操作记录
		opContent = opContent + "执行人为:" + selectById.getReportUser() + " 结束日期为:"
				+ df.format(selectById.getActualEndDate());
		EquipRepairOpRecordEntity equipRepairOpRecordEntity = new EquipRepairOpRecordEntity();
		equipRepairOpRecordEntity.setRecordContent(opContent);
		equipRepairOpRecordEntity.setRepairId(selectById.getId());
		baseEntityDataUtil.setBaseEntityData(equipRepairOpRecordEntity);
		int insert2 = equipRepairOpRecordDao.insert(equipRepairOpRecordEntity);
		if (insert2 < 1) {
			throw new RuntimeException("保存操作记录失败");
		}
		return resultBean;
	}

	// 分页查询
	public ResultDev selectByCondition(EquipRepair obj) {
		QueryWrapper<EquipRepairEntity> queryWrapper = new QueryWrapper<EquipRepairEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getPlanNumber() != null && !"".equals(obj.getInnerData().getPlanNumber())) {
				queryWrapper.like("plan_number", obj.getInnerData().getPlanNumber());
			}
			if (obj.getInnerData().getPlanType() != null && !"".equals(obj.getInnerData().getPlanType())) {
				queryWrapper.like("plan_type", obj.getInnerData().getPlanType());
			}
			if (obj.getInnerData().getDevNumber() != null && !"".equals(obj.getInnerData().getDevNumber())) {
				queryWrapper.like("dev_number", obj.getInnerData().getDevNumber());
			}
			if (obj.getInnerData().getDevName() != null && !"".equals(obj.getInnerData().getDevName())) {
				queryWrapper.like("dev_name", obj.getInnerData().getDevName());
			}
			if (obj.getInnerData().getDevWorkShop() != null && !"".equals(obj.getInnerData().getDevWorkShop())) {
				queryWrapper.like("dev_work_shop", obj.getInnerData().getDevWorkShop());
			}
			if (obj.getInnerData().getActualUser() != null && !"".equals(obj.getInnerData().getActualUser())) {
				queryWrapper.like("actual_user", obj.getInnerData().getActualUser());
			}
			if (obj.getFaultDateBegin() != null && obj.getFaultDateEnd() != null) {
				queryWrapper.between("fault_date", obj.getFaultDateBegin(), obj.getFaultDateEnd());
			}
			if (obj.getInnerData().getStatus() != 0) {
				queryWrapper.eq("status", obj.getInnerData().getStatus());
			}
			if (obj.getStatusList() != null&&obj.getStatusList().size()>0) {
				queryWrapper.in("status",obj.getStatusList());
			}
		}
		Page<EquipRepairEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipRepairEntity> iPage = equipRepairDao.selectPage(page, queryWrapper);
		List<EquipRepairEntity> list = iPage.getRecords();
		if (list == null || list.size() < 1) {
			return null;
		}
		ArrayList<EquipRepair> listEquipRepair = new ArrayList<EquipRepair>();
		for (EquipRepairEntity item : list) {
			EquipRepair equipRepair = new EquipRepair();
			equipRepair.setInnerData(item);
			if (item.getAttachmentGroupId() != 0) {
				List<QualityAttachmentGroup> listQualityAttachmentGroup = qualityAttachmentGroupDataHandler
						.getByAttachmentGroupId(item.getAttachmentGroupId());
				equipRepair.setListAttachmentGroup(listQualityAttachmentGroup);
			}
			listEquipRepair.add(equipRepair);
		}
		ResultDev resultDev = new ResultDev();
		resultDev.setEntityList(listEquipRepair);
		resultDev.setTotalQuantity((int) iPage.getTotal());
		resultDev.setPageCount((int) iPage.getPages());
		return resultDev;

	}

	// 委外确认
	public ResultBean outsourcingConfirm(EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		EquipRepairEntity innerData = obj.getInnerData();
		if (innerData.getId() == 0) {
			throw new RuntimeException("无法获取维修主键");
		}
		EquipRepairEntity selectById = equipRepairDao.selectById(innerData.getId());
		if (selectById == null) {
			throw new RuntimeException("无法获取维修数据");
		}
		if (StringUtils.isEmpty(innerData.getAuditDepartment())) {
			throw new RuntimeException("委外审核科室不能为空！");
		}
		if (StringUtils.isEmpty(innerData.getAuditUser())) {
			throw new RuntimeException("审核技术员不能为空！");
		}
		selectById.setSelfTaughtContent(innerData.getSelfTaughtContent());
		selectById.setApplyingReason(innerData.getApplyingReason());
		selectById.setApplyingContent(innerData.getApplyingContent());
		selectById.setAuditDepartment(innerData.getAuditDepartment());
		selectById.setAuditUser(innerData.getAuditUser());
		baseEntityDataUtil.updateBaseEntityData(selectById);
		equipRepairDao.updateById(selectById);
		return resultBean;
	}

	// 通过维修信息主键查询并打印pdf
	public Map<String,String> printRepairInfoPdf(long repairId) throws Exception {
		//EquipRepairEntity innerData = obj.getInnerData();
		QueryWrapper<EquipRepairEntity> queryWrapper = new QueryWrapper<EquipRepairEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("repair_info_id",repairId);
		EquipRepairEntity selectOne = equipRepairDao.selectOne(queryWrapper);
		if (selectOne == null) {
			throw new RuntimeException("通过维修主键无法查询维修信息！");
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("repairId",repairId+"");
		// 通过设备id查询对应设备信息
		QueryWrapper<EquipDevEntity> queryWrapper1 = new QueryWrapper<EquipDevEntity>();
		queryWrapper1.eq("row_status", 1);
		queryWrapper1.eq("dev_number", selectOne.getDevNumber());
		EquipDevEntity selectOne2 = equipDevDao.selectOne(queryWrapper1);
		map.put("equipName", selectOne2.getDevName());
		// 通过维修主键查询维修步骤数据
		QueryWrapper<EquipRepairStepEntity> queryWrapper2 = new QueryWrapper<EquipRepairStepEntity>();
		queryWrapper2.eq("row_status", 1);
		queryWrapper2.eq("repair_id", repairId);
		queryWrapper2.orderByAsc("step_sequence");
		List<EquipRepairStepEntity> selectList = equipRepairStepDao.selectList(queryWrapper2);
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("  ");
		if (selectList.size() > 0) {
			for (EquipRepairStepEntity equipRepairStepEntity : selectList) {
				stringBuilder.append(equipRepairStepEntity.getStepSequence()).append("  ")
						.append(equipRepairStepEntity.getRepairItem()).append("  ")
						.append(equipRepairStepEntity.getRepairContent());
				if (selectList.indexOf(equipRepairStepEntity) != selectList.size()-1) {
					// 获取系统默认换行符
					String lineSeparator = System.getProperty("line.separator", "\n");
					stringBuilder.append(lineSeparator);
				}
			}
		}
		map.put("repairStep", stringBuilder.toString());
		System.out.println(stringBuilder);
//		try {
//			Connection connection = baseConnectionData.getConnection();
////			// 生成PDF
////			JasperRunManager.runReportToPdfFile("src/main/resources/config/repair.jasper",
////					"F:\\Download\\repair.pdf", map, connection);
////			FileInputStream fileInputStream = new FileInputStream(
////					new File("src/main/resources/config/repair.jasper"));
////			runReportToPdf = JasperRunManager.runReportToPdf(fileInputStream, map, connection);
//		} catch (JRException e) {
//			e.printStackTrace();
//		}
		return map;
	}

	public EquipExecuteCount queryExecuteCount(){
		EquipExecuteCount count=new EquipExecuteCount();
		QueryWrapper<EquipRepairEntity> repair = new QueryWrapper<EquipRepairEntity>();
		repair.eq("row_status", 1);
		repair.eq("status", MomConstants.EQUIP_REPAIR_STATUS_2);
		count.setRepairCount(equipRepairDao.selectCount(repair));
		QueryWrapper<EquipPatrolCheckWorkOrderEntity> patrolCheck = new QueryWrapper<EquipPatrolCheckWorkOrderEntity>();
		patrolCheck.eq("row_status", 1);
		patrolCheck.eq("status",0);
		count.setRepairInspectionCount(equipPatrolCheckWorkOrderDao.selectCount(patrolCheck));
		QueryWrapper<EquipMaintainWorkOrderEntity> maintain = new QueryWrapper<EquipMaintainWorkOrderEntity>();
		maintain.eq("row_status", 1);
		maintain.eq("status", MomConstants.EQUIP_MAINTAIN_ORDER_STATUS_NOEXECUTE);
		count.setMaintainCount(equipMaintainWorkOrderDao.selectCount(maintain));
		QueryWrapper<EquipRegularCheckWorkOrderEntity> check = new QueryWrapper<EquipRegularCheckWorkOrderEntity>();
		check.eq("row_status", 1);
		check.eq("status", MomConstants.EQUIP_REGULAR_CHECK_ORDER_STATUS_NOEXECUTE);
		count.setCheckCount(equipRegularCheckWorkOrderDao.selectCount(check));
		return count;
	}

}
